package com.acme.controller;

import javax.transaction.Transactional;

import com.acme.entity.Claim;
import com.acme.entity.User;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.acme.dao.ClaimDAO;

@Transactional
@Controller
@RequestMapping(value = "InsuranceAdjuster")
public class InsuranceAdjusterController {
	@Autowired
	private ClaimDAO claimDAO;
	
	@Autowired @Qualifier("claimApprovedEmail")
	private SimpleMailMessage claimApprovedEmail;
	
	@Autowired @Qualifier("claimDeniedEmail")
	private SimpleMailMessage claimDeniedEmail;
	
	@Autowired
	private JavaMailSender mailSender;
	
	@RequestMapping(method = RequestMethod.GET)
	public ModelAndView admin(){
		ModelAndView model = new ModelAndView();
		model.getModelMap().addAttribute("submittedClaims", claimDAO.getClaimsSubmmitted());
		return model;
	}
	@RequestMapping(method = RequestMethod.POST, params={"Approved"})
	public ModelAndView processClaimApprove(@RequestParam(value= "Approved", required= true)String idclaim){
		
		String idOnly=idclaim.replaceAll("[^0-9]","");
		
		Claim claim = claimDAO.getClaimByID(Integer.parseInt(idOnly));
		claim.setStatus("Approved");
		claimDAO.updateClaim(claim);
		sendApprovedEmail(claim);
		ModelAndView model = new ModelAndView();
		model.getModelMap().addAttribute("submittedClaims", claimDAO.getClaimsSubmmitted());
		admin();
		return model;
	}
	@RequestMapping(method = RequestMethod.POST, params={"Denied"})
	public ModelAndView processClaimDeny(@RequestParam(value= "Denied", required= true) String idclaim){
		String idOnly=idclaim.replaceAll("[^0-9]","");
		Claim claim = claimDAO.getClaimByID(Integer.parseInt(idOnly));
		claim.setStatus("Denied");
		claimDAO.updateClaim(claim);
		sendDeniedEmail(claim, "We don't like your claim!");
		ModelAndView model = new ModelAndView();
		model.getModelMap().addAttribute("submittedClaims", claimDAO.getClaimsSubmmitted());
		admin();
		return model;
	}
	
	private void sendApprovedEmail(Claim claim) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(claimApprovedEmail.getFrom());
		User user = claim.getUser();
		message.setTo(user.getEmail());
		String name = user.getfName() + " " + user.getlName();
		String claimNumber = Integer.toString(claim.getClaimID());
		message.setText(String.format(claimApprovedEmail.getText(), name, claimNumber));
		mailSender.send(message);
	}
	
	private void sendDeniedEmail(Claim claim, String reason) {
		SimpleMailMessage message = new SimpleMailMessage();
		message.setFrom(claimDeniedEmail.getFrom());
		User user = claim.getUser();
		message.setTo(user.getEmail());
		String name = user.getfName() + " " + user.getlName();
		String claimNumber = Integer.toString(claim.getClaimID());
		message.setText(String.format(claimDeniedEmail.getText(), name, claimNumber, reason, "https://192.168.3.153:8080/AcmeClaims/MyAccount"));
		mailSender.send(message);
	}
}
